<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>延时队列：时间维度的消息调度艺术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% - 20px);
            background: #e0e7ff;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        .code-block {
            background: #1e293b;
            color: #e2e8f0;
            border-radius: 8px;
            padding: 20px;
            font-family: 'Consolas', 'Monaco', monospace;
            overflow-x: auto;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    延时队列
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    时间维度的消息调度艺术
                </p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-clock mr-2"></i>
                        <span>精准时控</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-layer-group mr-2"></i>
                        <span>灵活调度</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-sync-alt mr-2"></i>
                        <span>可靠传递</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <div class="content-card rounded-2xl shadow-xl p-10">
                    <p class="text-lg leading-relaxed text-gray-700">
                        <span class="drop-cap">随</span>着互联网应用的不断发展，我们面对的用户需求和业务场景也愈加复杂。在这种背景下，如何有效地管理和调度系统中的各种任务，成为了一个重要的挑战。比如，如何在用户下单后的一段时间内发送确认邮件，如何在用户未支付订单达到一定时间后自动取消订单，如何在系统出现故障时延迟重试任务，等等。这些需求都需要我们在一定的时间延迟后再执行某些操作，而不是立即进行处理。
                    </p>
                    <p class="text-lg leading-relaxed text-gray-700 mt-6">
                        这时候，延时队列就派上了用场。延时队列是一种特殊的消息队列，它允许我们将消息发送到队列后，设定一个延迟时间，只有在这个延迟时间过后，消息才会被处理。这样，我们就可以灵活地控制任务的执行时间，从而实现很多复杂的业务逻辑。
                    </p>
                </div>
            </div>
        </div>
    </section>

    <!-- Core Concept Section -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">延时队列的定义与特性</h2>
                
                <div class="grid md:grid-cols-2 gap-8 mb-12">
                    <div class="content-card rounded-xl shadow-lg p-8">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800">
                            <i class="fas fa-info-circle feature-icon mr-2"></i>
                            核心定义
                        </h3>
                        <p class="text-gray-600 leading-relaxed">
                            延时队列是一种特殊的消息队列，它允许消息在被发送后经过一个预定的延迟时间才被消费者处理。与传统消息队列不同，延时队列通过引入时间维度，使得消息的处理时机得以灵活控制。
                        </p>
                    </div>
                    
                    <div class="content-card rounded-xl shadow-lg p-8">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800">
                            <i class="fas fa-star feature-icon mr-2"></i>
                            基本特性
                        </h3>
                        <ul class="space-y-3 text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-3"></i>
                                <span><strong>延迟处理：</strong>消息需等待指定时间才能被消费</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-3"></i>
                                <span><strong>时间敏感性：</strong>满足时间敏感的业务需求</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-3"></i>
                                <span><strong>灵活性：</strong>支持多种延迟时间配置</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-3"></i>
                                <span><strong>消息调度：</strong>定期检查并处理到期消息</span>
                            </li>
                        </ul>
                    </div>
                </div>

                <!-- Comparison Table -->
                <div class="content-card rounded-xl shadow-lg p-8">
                    <h3 class="text-xl font-semibold mb-6 text-gray-800">
                        <i class="fas fa-balance-scale feature-icon mr-2"></i>
                        延时队列 vs 普通队列
                    </h3>
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead>
                                <tr class="border-b-2 border-purple-200">
                                    <th class="text-left py-3 px-4 text-gray-700">特性</th>
                                    <th class="text-left py-3 px-4 text-gray-700">普通队列</th>
                                    <th class="text-left py-3 px-4 text-gray-700">延时队列</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="border-b border-gray-200">
                                    <td class="py-3 px-4 font-medium">处理时机</td>
                                    <td class="py-3 px-4 text-gray-600">消息入队即可消费</td>
                                    <td class="py-3 px-4 text-gray-600">需等待延迟时间后消费</td>
                                </tr>
                                <tr class="border-b border-gray-200">
                                    <td class="py-3 px-4 font-medium">业务逻辑</td>
                                    <td class="py-3 px-4 text-gray-600">侧重实时性</td>
                                    <td class="py-3 px-4 text-gray-600">支持定时任务、超时重试</td>
                                </tr>
                                <tr>
                                    <td class="py-3 px-4 font-medium">实现复杂度</td>
                                    <td class="py-3 px-4 text-gray-600">相对简单</td>
                                    <td class="py-3 px-4 text-gray-600">需要额外的调度机制</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Working Principle Section -->
    <section class="py-16">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">延时队列的工作原理</h2>
                
                <div class="content-card rounded-xl shadow-lg p-10 mb-12">
                    <div class="mermaid">
                        graph LR
                            A[生产者] -->|发送延时消息| B[延时队列]
                            B -->|存储消息| C[消息存储]
                            C -->|时间调度| D[调度器]
                            D -->|检查到期消息| E[普通队列]
                            E -->|消费| F[消费者]
                            
                            style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                            style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                            style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                            style D fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                            style E fill:#43e97b,stroke:#fff,stroke-width:2px,color:#fff
                            style F fill:#fa709a,stroke:#fff,stroke-width:2px,color:#fff
                    </div>
                </div>

                <div class="space-y-8">
                    <div class="timeline-item">
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">1. 消息存储</h3>
                        <p class="text-gray-600 leading-relaxed">
                            当消息被发送到延时队列时，系统会记录下消息的内容、入队时间以及预设的延迟时间。常用的数据结构包括优先级队列或有序列表，这些结构可以有效管理延时消息，根据到期时间进行排序。
                        </p>
                    </div>

                    <div class="timeline-item">
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">2. 时间调度</h3>
                        <p class="text-gray-600 leading-relaxed">
                            延时队列通常依赖定时任务或调度器来定期检查消息的